package indi.yuluo.controller;

import indi.yuluo.entity.DTO.UserCommentsWithSignatureDto;
import indi.yuluo.entity.discussion.Discussion;
import indi.yuluo.entity.user.UserInfo;
import indi.yuluo.service.DiscussService;
import indi.yuluo.service.UserService;
import indi.yuluo.vo.Msg;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @Author: yuluo
 * @CreateTime: 2022-08-21  20:25
 * @Description: TODO
 */

@Slf4j
@Api("讨论信息相关控制器")
@RestController
@RequestMapping("/discuss")
public class DiscussController {

    @Resource
    private DiscussService discussService;

    @Resource
    private UserService userService;

    /**
     *
     * @param pid
     * @return
     */
    @GetMapping("/getDiscussInfoByPId")
    @ApiOperation(value = "根据题目id查询评论信息", httpMethod = "GET")
    public Msg getDiscussInfoByPId(@RequestParam(value = "pid") Long pid) {

        List<UserCommentsWithSignatureDto> list = discussService.getDiscussInfoByPId(pid);

        return Msg.success().add("disList", list);
    }

    /**
     * 保存用户评论信息
     * @param currentProblemId
     * @param token
     * @param content
     * @return
     */
    @PostMapping("/release")
    @ApiOperation(value = "保存用户评论信息", httpMethod = "POST")
    public Msg release(
            @RequestParam(value = "pid") Long currentProblemId,
            @RequestParam(value = "token") String token,
            @RequestParam(value = "content") String content
    ) {
        boolean flag =  discussService.saveComments(currentProblemId, token, content);
        return flag ? Msg.success() : Msg.fail();
    }

    /**
     * 用户点赞
     * 可以反复点击，已经点赞过了，再次点击取消，未点则加入
     * @param did  讨论id
     * @param token 用户登录凭证
     * @return 返回值
     */
    @GetMapping("/like")
    @ApiOperation(value="用户点赞")
    public Msg like(
            @RequestParam(value = "did") Long did,
            @RequestParam(value = "token") String token
    ) {

        // key代表数据库的删除和插入操作是否成功
        // value为1000是删除，2000是插入
        Map<Boolean, Integer> like = discussService.like(did, token);

        Integer data = 0;
        boolean flag = false;

        for (Map.Entry<Boolean, Integer> next : like.entrySet()) {
            flag = next.getKey();
            data = next.getValue();
        }

        return flag ? Msg.success().add("data", data) : Msg.fail();
    }

    /**
     * 查询用户的点赞状态
     *
     * @param did
     * @param token
     * @return
     */
    @GetMapping("/checkLikesStatus")
    @ApiOperation(value = "查询用户点赞状态", httpMethod = "GET")
    public Msg checkLikesStatus(
            @RequestParam(value = "did") Long did,
            @RequestParam(value = "token") String token
    ) {
        return discussService.checkLikesStatus(did, token) ? Msg.success() : Msg.fail();
    }

    /**
     * 删除评论
     *
     * @param did
     * @param token
     * @return
     */
    @GetMapping("/del")
    @ApiOperation(value = "删除评论", notes = "只能删除自己发布的评论")
    public Msg del(
            @RequestParam(value = "did") Long did,
            @RequestParam(value = "token") String token
    ) {

        boolean flag = false;

        int i = discussService.del(did, token);

        if (i != 1000) {
            flag = true;
        }

        return flag ? Msg.success() : Msg.fail().add("data", "不是您发布的评论，不能删除哦！");
    }

}
